package cn.kgm.task;

/**
 * Create by kappi on
 * 2021/8/5/005 20:21
 */

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;

@Component
public class SpringProducterTask {

    @Resource
    private DefaultMQProducer producer;

    /**
     * 每5秒执行一次
     */
    @Scheduled(cron = "0/5 * *  * * ?" )
    private void sendMsgToMq() {
        String str = "发送测试消息";
        Message msg;
        try {
            msg = new Message("kagim-topic-v2"
                    , "kagim-topic-v1-tag2"
                    , UUID.randomUUID().toString()
                    , str.getBytes("utf-8" ));
            SendResult result = producer.send(msg);
            if (result.getSendStatus() == SendStatus.SEND_OK) {
                System.out.println("消息发送成功" );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//
//    /**
//     * 每5秒执行一次
//     */
//    @Scheduled(cron = "0/5 * *  * * ?")
//    private void getMsgToMq() {
//        DefaultMQPushConsumer consumer =  new DefaultMQPushConsumer("Kagim-RocketMQ-v1");
//        String str = "发送测试消息";
//        Message msg;
//        try {
//            msg = new Message("N6VLK9R0X1GNJRY"
//                    , "Kagim-topit-tags1-v1"
//                    , UUID.randomUUID().toString()
//                    , str.getBytes("utf-8"));
//            String namespace = consumer.getNamespace();
//            if (true) {
//                System.out.println(namespace);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
